/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { DatePicker } from 'antd';
import { tomorrow } from './utils';
import moment from 'moment';

interface RANGEPICKER {
  setDates: Function;
  dates: string[] | any; //diff
  range: string;
  defaultValue?: any;
}

const { RangePicker } = DatePicker;

const Index = ({
  setDates,
  dates,
  range = '7',
  defaultValue = [null, null],
  ...rest
}: RANGEPICKER) => {
  const disabledDate = (current: any) => {
    if (!dates || dates.length === 0) {
      if (current) return current > moment().subtract(1, 'days');
    }
    const tooLate = dates[0] && current.diff(dates[0], 'days') > range;
    const tooEarly = (dates[1] && dates[1].diff(current, 'days') > range) || current >= tomorrow;
    return tooEarly || tooLate;
  };
  return (
    <RangePicker
      {...rest}
      placeholder={['开始时间', '结束时间']}
      disabledDate={disabledDate}
      onCalendarChange={(val: any) => setDates(val)}
      format='YYYY-MM-DD'
      defaultValue={defaultValue}
    />
  );
};

export default Index;
